import { Address, IUserAddressesResponse } from '../inteface/user'
import { get, post } from '../utils/request'
import { IResponse } from './base'

export const getUserAddresses = async (): Promise<IUserAddressesResponse | undefined> => {
  try {
    const data = await get<IUserAddressesResponse>('/api/user/address-list')
    return data
  } catch (error) {
    console.error('获取用户地址失败:', error)
    throw error
  }
}

// /delete-address  删除
// /set-address-default 设置默认

export const deleteAddress = async (id: string): Promise<IResponse<any> | undefined> => {
  try {
    const data = await post<IResponse<any>>(`/api/user/delete-address`, { id })
    return data
  } catch (error) {
    console.error('删除用户地址失败:', error)
    throw error
  }
}

export const setAddressDefault = async (id: string): Promise<IResponse<any> | undefined> => {
  try {
    const data = await post<IResponse<any>>(`/api/user/set-address-default`, { id })
    return data
  } catch (error) {
    console.error('设置默认地址失败:', error)
    throw error
  }
}

// get api/user/address
export const getAddress = async (id: string): Promise<IResponse<any> | undefined> => {
  try {
    const data = await get<IResponse<any>>(`/api/user/address`, { data: { id } })
    return data
  } catch (error) {
    console.error('获取地址失败:', error)
    throw error
  }
}

export const addAddress = async (data: Omit<Address, 'id'>): Promise<IResponse<any> | undefined> => {
  try {
    const res = await post<IResponse<any>>(`/api/user/add-address`, data)
    return res
  } catch (error) {
    console.error('新增地址失败:', error)
    throw error
  }
}
